package com.starlink.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.starlink.common.PageInfoTwo;
import com.starlink.common.utils.AssertUtil;
import com.starlink.context.SessionContext;
import com.starlink.mapper.AddressMapper;
import com.starlink.model.domain.AddressDO;
import com.starlink.model.dtomapper.AddressDtoMapper;
import com.starlink.model.request.AddressRequest;
import com.starlink.model.util.AddressConvert;
import com.starlink.model.vo.AddressVO;
import com.starlink.service.AddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author zhanghong
 */
@Service
public class AddressServiceImpl extends ServiceImpl<AddressMapper, AddressDO> implements AddressService {
    @Autowired
    private AddressMapper addressMapper;

    @Override
    public AddressVO getByKey(long id) {
        AddressDO addressDO = getById(id);
        AddressVO addressVO = AddressDtoMapper.INSTANCE.do2VO(addressDO);
        return addressVO;
    }

    @Override
    public PageInfoTwo<AddressVO> pageQuery(AddressRequest request) {
        request.setCreateUser(SessionContext.getWechatUserId());
        long count = addressMapper.pageCount(request);
        List<AddressDO> list = addressMapper.pageQuery(request, request.getPageSize(), request.fetchOffSet());
        List<AddressVO> addressVOList = AddressConvert.buildAddressVoList(list);
        return new PageInfoTwo(request.getPageNum(), request.getPageSize(), addressVOList, count);
    }

    @Override
    public List<AddressVO> listQuery(AddressRequest request) {
        List<AddressDO> list = addressMapper.selectDOList(AddressDtoMapper.INSTANCE.request2DO(request));
        List<AddressVO> addressVOS = AddressConvert.buildAddressVoList(list);
        return addressVOS;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Boolean add(AddressRequest request) {
        request.setCreateUser(SessionContext.getWechatUserId());
        AddressDO addressDO = AddressDtoMapper.INSTANCE.request2DO(request);
        if (request.getDefaultAddr() == 1) {
            addressMapper.resetDefaultAddr(SessionContext.getWechatUserId());
        }
        saveOrUpdate(addressDO);
        return true;
    }

    @Override
    public Boolean updateByKey(AddressRequest request) {
        AssertUtil.assertNotNull(request.getId(), "id不能为空");
        request.setUpdateUser(SessionContext.getWechatUserId());
        AddressDO addressDO = AddressDtoMapper.INSTANCE.request2DO(request);
        if (request.getDefaultAddr() == 1) {
            addressMapper.resetDefaultAddr(SessionContext.getWechatUserId());
        }
        saveOrUpdate(addressDO);
        return true;
    }

    @Override
    public Boolean deleteByKey(long id) {
        addressMapper.deleteDOById(id);
        return true;
    }

    @Override
    public String selectInfoById(Long addressId) {
        return addressMapper.selectInfoById(addressId);
    }
}
